//
//  CartPoPViewController.m
//  TestByFF
//
//  Created by hanxifeng on 16/5/10.
//  Copyright © 2016年 haixu. All rights reserved.
//

#import "CartPoPViewController.h"
#import "CartTableViewCell.h"
#import "POP.h"
@interface CartPoPViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)CALayer *layer;
@property (nonatomic,weak)UIImageView *rightItem;
@end

@implementation CartPoPViewController{

    UITableView *_tableView;
    NSMutableArray *_dataArray;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    //navigationitem
    [self buildNavigationItem];
    //tableview
    [self buildTableView];
}
-(void)buildNavigationItem{
    _dataArray = [[NSMutableArray alloc] init];
    int temp = 0;
    for (int i=1; i<=20; i++) {
        temp = i;
        if (temp>10) {
            temp=i-10;
        }
        NSDictionary *dataDic = [[NSDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%d",temp],@"iconimg", nil];
        [_dataArray addObject:dataDic];
    }
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 20, 20)];
    self.rightItem = img;
    img.image = [UIImage imageNamed:@"cartPop"];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:img];
}
-(void)buildTableView{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    _tableView.delegate = self;
    _tableView.dataSource= self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.rowHeight = 50;
    [self.view addSubview:_tableView];
}


#pragma mark - delegate
#pragma mark tableview delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 20;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CartTableViewCell *cell = [CartTableViewCell customCellWithTableView:tableView];
    
    cell.dataDic = _dataArray[indexPath.row];
    
    __weak CartPoPViewController *weakSelf = self;
    [cell cartBtnClickBlock:^(UIImageView *img, UITableViewCell *cell) {
        [weakSelf cartBtnClick:img andCell:cell];
    }];
    
    return cell;
}
#pragma mark animationdelegate
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    [self.layer removeFromSuperlayer];
    self.layer = nil;
    
    //购物车抖动
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    basicAnimation.fromValue = @(-M_PI_4);
    basicAnimation.toValue = @(M_PI_4);
    basicAnimation.duration = 10;
    basicAnimation.repeatCount = 3;
    basicAnimation.autoreverses = YES;
    [self.rightItem.layer addAnimation:basicAnimation forKey:@"shakeAnimation"];
    
    _tableView.userInteractionEnabled = YES;
}
#pragma mark - action
#pragma mark click action
-(void)cartBtnClick:(UIImageView *)img andCell:(UITableViewCell *)cell{
    NSIndexPath *indexPath = [_tableView indexPathForCell:cell];
    //cell在整个tableview上的rect
    CGRect rect = [_tableView rectForRowAtIndexPath:indexPath];
    //cell在整个背景view（tableview的父view）上的rect  可用来当作layer在背景view上的坐标
    CGRect inSuperViewRect = [_tableView convertRect:rect toView:[_tableView superview]];
    CALayer *layer = [CALayer layer];
    self.layer = layer;
    layer.frame = CGRectMake(20, inSuperViewRect.origin.y + 64, 50, 50);
    layer.contents = img.layer.contents;
    [[UIApplication sharedApplication].keyWindow.layer addSublayer:layer];
    
    CGFloat all = 1;//动画全程1秒
    CGFloat big = 0.3;//放大图片持续0.3秒
    
    //放大图片动画
    CABasicAnimation *bigBasicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    bigBasicAnimation.duration = big;
    bigBasicAnimation.toValue = @(2);
    
    //旋转动画
    CABasicAnimation *rotationBasicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotationBasicAnimation.duration = all/10;//单次旋转所需时间
    rotationBasicAnimation.fromValue = @(-M_PI);
    rotationBasicAnimation.toValue = @(M_PI);
    rotationBasicAnimation.repeatCount = 10*all;
    
    //抛物线动画
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(20, inSuperViewRect.origin.y + 64)];
    [path addQuadCurveToPoint:CGPointMake(SCREEN_WIDTH - 30, 30) controlPoint:CGPointMake(SCREEN_WIDTH/3*2, -30)];
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyAnimation.path = path.CGPath;
    keyAnimation.duration = all;
    //根据抛物角度 旋转图片
//    keyAnimation.rotationMode = kCAAnimationRotateAuto;
    
    
    //缩小图片动画
    CABasicAnimation *smallBasicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    smallBasicAnimation.beginTime = big;
    smallBasicAnimation.duration = all-big;
    smallBasicAnimation.fromValue = @(2);
    smallBasicAnimation.toValue = @(0.5);
    
    
    //组动画
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.delegate = self;
    group.duration = all;
    group.animations = @[bigBasicAnimation,rotationBasicAnimation,keyAnimation,smallBasicAnimation];

    [layer addAnimation:group forKey:@"groupAnimation"];
    
    _tableView.userInteractionEnabled = NO;
}
@end
